// JavaScript Document
var SDFollow = {
    //obj:关注的DOM对象 concern:要关注的用户名 async:是否异步 loginName 登录用户名 isHide:如果是以关注或互相关注状态时是否隐藏按钮
    init: function(obj, concern, loginName, isHide, async) {
        if (loginName == null || loginName == "" || loginName == "undefined") {
            obj.removeClass();
            obj.html("加关注");
            obj.addClass("follow-none-btn");
            obj.show();
            return "1";
        }
        //如要返回正确的关注状态则ajax请求必须是同步的
        var resultData = "1";
        var result = true;

        if (typeof (async) == "undefined")
            result = true;
        else
            result = async;
        // alert(result);

        if (typeof (isHide) == "undefined")
            isHide = false;

        var ajax = {
            url: "/home/friend/GetConcernMessage", type: "POST",
            data: { concernName: concern },
            async: result,
            success: function(data) {
                resultData = data;
                obj.removeClass();
                //alert(resultData);
                //返回关注的文本 1:+关注 2:已关注 3:相互关注
                if (data == "1") {
                    obj.html("加关注");
                    obj.addClass("follow-none-btn");
                    obj.show();
                }
                else if (data == "2") {
                    obj.html("已关注");
                    obj.addClass("follow-success-btn");
                    if (!isHide)
                        obj.show();
                    else
                        obj.hide();
                }
                else if (data == "3") {
                    obj.html("互相关注");
                    obj.addClass("follow-together-btn");
                    if (!isHide)
                        obj.show();
                    else
                        obj.hide();
                }
            } //, error: function() { alert("d"); }
        }
        $.ajax(ajax);
        return resultData;
    },
    //obj:关注的DOM对象 otherObj:要同步的其他DOM concern:要关注的用户名 async:是否异步 loginName 登录用户名 isHide:如果是以关注或互相关注状态时是否隐藏按钮
    init_ForJsonp: function(objs, concern, loginName, isHide, async) {

        if (loginName == null || loginName == "" || loginName == "undefined") {
            for (var i = 0; i < objs.length; i++) {
                objs[i].removeClass();
                objs[i].html("加关注");
                objs[i].addClass("follow-none-btn");
                objs[i].show();
            }
            return "1";
        }

        //如要返回正确的关注状态则ajax请求必须是同步的
        var resultData = "1";
        var result = true;

        if (typeof (async) == "undefined")
            result = true;
        else
            result = async;
        // alert(result);

        if (typeof (isHide) == "undefined")
            isHide = false;

        var ajax = {
            url: "http://www.sodao.com/home/friend/GetConcernMessage4JSONP?jsonp=?", type: "GET",
            data: { concernName: concern },
            async: result,
            dataType: "jsonp",
            success: function(data) {
                resultData = data.status;
                for (var i = 0; i < objs.length; i++)
                    objs[i].removeClass();

                //alert(resultData);
                //返回关注的文本 1:+关注 2:已关注 3:相互关注
                if (data.status == "1") {
                    for (var i = 0; i < objs.length; i++) {
                        objs[i].html("加关注");
                        objs[i].addClass("follow-none-btn");
                        objs[i].show();

                    }
                }
                else if (data.status == "2") {
                    for (var i = 0; i < objs.length; i++) {
                        objs[i].html("已关注");
                        objs[i].addClass("follow-success-btn");

                        if (!isHide) {
                            objs[i].show();
                        }
                        else {
                            objs[i].hide();
                        }
                    }
                }
                else if (data.status == "3") {
                    for (var i = 0; i < objs.length; i++) {
                        objs[i].html("互相关注");
                        objs[i].addClass("follow-together-btn");

                        if (!isHide) {
                            objs[i].show();
                        }
                        else {
                            objs[i].hide();
                        }
                    }
                }
            } //, error: function() { alert("错误"); }
        }
        $.ajax(ajax);
        return resultData;
    },
    // concern:要关注的用户名 async:是否异步
    //objs:要同步状态的其他DOM对象 争对一个页面有多个关注按钮(关注同个人 并非是关注列表页)
    //otherObj:个人首页其他一个关注按钮（表现形式不同 如已关注则隐藏）
    init_V2: function(objs, otherObj, concern, loginName, async) {
        if (loginName == null || loginName == "" || loginName == "undefined") {
            for (var i = 0; i < objs.length; i++) {
                var obj = objs[i];
                obj.removeClass();
                obj.html("加关注");
                obj.addClass("follow-none-btn");
                obj.show();
            }
            otherObj.removeClass();
            otherObj.html("加关注");
            otherObj.addClass("follow-none-btn");
            otherObj.show();
            return;
        }
        var result = true;
        if (typeof (async) == "undefined")
            result = true;
        else
            result = async;
        var ajax = {
            url: "/home/friend/GetConcernMessage", type: "POST",
            data: { concernName: concern },
            async: result,
            success: function(data) {
                for (var i = 0; i < objs.length; i++) {
                    var obj = objs[i];
                    obj.removeClass();
                }
                otherObj.removeClass();
                //alert(data);
                //返回关注的文本 1:+关注 2:已关注 3:相互关注
                if (data == "1") {
                    for (var i = 0; i < objs.length; i++) {
                        var obj = objs[i];
                        obj.html("加关注");
                        obj.addClass("follow-none-btn");
                        obj.show();
                    }
                    otherObj.html("加关注");
                    otherObj.addClass("follow-none-btn");
                    otherObj.show();
                }
                else if (data == "2") {
                    for (var i = 0; i < objs.length; i++) {
                        var obj = objs[i];
                        obj.html("已关注");
                        obj.addClass("follow-success-btn");
                        obj.show();
                    }
                    otherObj.hide();
                }
                else if (data == "3") {
                    for (var i = 0; i < objs.length; i++) {
                        var obj = objs[i];
                        obj.html("互相关注");
                        obj.addClass("follow-together-btn");
                        obj.show();
                    }
                    otherObj.hide();
                }
            } //, error: function() { alert("d"); }
        }
        $.ajax(ajax);
    },
    // concern:要关注的用户名的DOM对象的属性 async:是否异步
    //objs:关注按钮列表DOM对象
    init_V3: function(objs, concern, loginName, async) {
        if (loginName == null || loginName == "" || loginName == "undefined") {
            for (var i = 0; i < objs.length; i++) {
                var obj = objs[i];
                obj.removeClass();
                obj.html("加关注");
                obj.addClass("follow-none-btn");
                obj.show();

            }
            return;
        }
        var result = true;
        if (typeof (async) == "undefined")
            result = true;
        else
            result = async;
        var list = "";
        for (var i = 0; i < objs.length; i++) {
            var obj = objs[i];
            var cnName = obj.attr(concern);
            list += cnName + ",";
        }
        var ajax = {
            url: "/home/friend/GetConcernMessage4List", type: "POST",
            data: { concernNameList: list },
            dataType: "json",
            async: result,
            success: function(datas) {
                for (var i = 0; i < objs.length; i++) {
                    var obj = objs[i];
                    obj.removeClass();

                }
                for (var k = 0; k < datas.length; k++) {
                    var data = datas[k];
                    //alert(data);
                    var obj = objs[k];
                    obj.removeClass();
                    //返回关注的文本 1:+关注 2:已关注 3:相互关注
                    if (data == "1") {
                        obj.html("加关注");
                        obj.addClass("follow-none-btn");
                        obj.show();

                    }
                    else if (data == "2") {
                        obj.html("已关注"); //alert(obj.html());
                        obj.addClass("follow-success-btn");
                        obj.show();

                    }
                    else if (data == "3") {
                        obj.html("互相关注");
                        obj.addClass("follow-together-btn");
                        obj.show();

                    }
                }
                try {
                    //我看过谁 谁看过我有此方法
                    loadFriendInfo();
                } catch (e) {
                }
            } //, error: function() { alert("d"); }
        }
        $.ajax(ajax);
    },
    init_V3ForJsonp: function(objs, concern, loginName, async) {
        if (loginName == null || loginName == "" || loginName == "undefined") {
            for (var i = 0; i < objs.length; i++) {
                var obj = objs[i];
                obj.removeClass();
                obj.html("加关注");
                obj.addClass("follow-none-btn");
                obj.show();

            }
            return;
        }
        var result = true;
        if (typeof (async) == "undefined")
            result = true;
        else
            result = async;
        var list = "";
        for (var i = 0; i < objs.length; i++) {
            var obj = objs[i];
            var cnName = obj.attr(concern);
            list += cnName + ",";
        }
        var ajax = {
            url: "http://www.sodao.com/home/friend/GetConcernMessage4ListJSONP?jsonp=?", type: "POST",
            data: { concernNameList: list },
            dataType: "jsonp",
            async: result,
            success: function(datas) {
                for (var i = 0; i < objs.length; i++) {
                    var obj = objs[i];
                    obj.removeClass();

                }
                for (var k = 0; k < datas.length; k++) {
                    var data = datas[k];
                    //alert(data);
                    var obj = objs[k];
                    obj.removeClass();
                    //返回关注的文本 1:+关注 2:已关注 3:相互关注
                    if (data == "1") {
                        obj.html("加关注");
                        obj.addClass("follow-none-btn");
                        obj.show();

                    }
                    else if (data == "2") {
                        obj.html("已关注"); //alert(obj.html());
                        obj.addClass("follow-success-btn");
                        obj.show();

                    }
                    else if (data == "3") {
                        obj.html("互相关注");
                        obj.addClass("follow-together-btn");
                        obj.show();

                    }
                }
                try {
                    //我看过谁 谁看过我有此方法
                    loadFriendInfo();
                } catch (e) {
                }
            } //, error: function() { alert("d"); }
        }
        $.ajax(ajax);
    },
    //新人榜 初始化状态 只初始化文字和click事件
    init_V4: function(objs, concern, loginName, async) {
        if (loginName == null || loginName == "" || loginName == "undefined") {
            for (var i = 0; i < objs.length; i++) {
                var obj = objs[i];
                obj.html("加关注");
                obj.bind("click", function(event) {
                    checkLogin(loginName);
                    event.preventDefault();
                });
                obj.show();

            }
            return;
        }
        var result = true;
        if (typeof (async) == "undefined")
            result = true;
        else
            result = async;
        var list = "";
        for (var i = 0; i < objs.length; i++) {
            var obj = objs[i];
            var cnName = obj.attr(concern);
            list += cnName + ",";
        }
        var ajax = {
            url: "/home/friend/GetConcernMessage4List", type: "POST",
            data: { concernNameList: list },
            dataType: "json",
            async: result,
            success: function(datas) {
                for (var k = 0; k < datas.length; k++) {
                    var data = datas[k];
                    //alert(data);
                    var obj = objs[k];
                    //返回关注的文本 1:+关注 2:已关注 3:相互关注
                    if (data == "1") {
                        obj.html("加关注");
                        obj.bind("click", function(event) {
                            addConcern($(this).attr(concern), $(this));
                            event.preventDefault();
                        });
                        obj.show();
                    }
                    else if (data == "2") {
                        obj.html("已关注"); //alert(obj.html());
                        obj.bind('click', function(event) {
                            event.preventDefault();
                        });
                        obj.show();
                    }
                    else if (data == "3") {
                        obj.html("已关注");
                        obj.bind('click', function(event) {
                            event.preventDefault();
                        });
                        obj.show();
                    }
                }
            } //, error: function() { alert("d"); }
        }
        $.ajax(ajax);
    },
    addFollow: function(that, concernName, visitUser) {//visitUser:当前登录用户名
        //alert(visitUser);
        if (!checkLogin(visitUser)) {//checkLogin函数 在jquery.dialog.sodao.js里
            return;
        }

        $.ajax({
            url: "http://www.sodao.com/home/friend/IsFriendOutExist?jsonp=?",
            type: "GET",
            dataType: "jsonp",
            data: { "tousername": concernName },
            success: function(data) {
                if (data.status == 0) {
                    $.ajax({
                        url: "http://www.sodao.com/home/friend/ConcernAdd_V2?jsonp=?",
                        type: "GET",
                        dataType: "jsonp",
                        data: { "concernName": concernName },
                        success: function(data) {
                            var tipContent = "";
                            switch (data.status) {
                                case 1:
                                    tipContent = '<div class="SD-follow-box"><img class="tipicon tip1" src="http://css.sodao.com/home/img/space.png" /><p>关注成功。</p></div>';
                                    break;
                                case 2:
                                    tipContent = '<div class="SD-follow-box"><img class="tipicon tip1" src="http://css.sodao.com/home/img/space.png" /><p>关注成功。</p></div>';
                                    break;
                                case 0:
                                    tipContent = '<div class="SD-follow-box"><img class="tipicon tip0" src="http://css.sodao.com/home/img/space.png" /><p>关注失败。</p></div>';
                                    break;
                                case -1:
                                    tipContent = '<div class="SD-follow-box"><img class="tipicon tip0" src="http://css.sodao.com/home/img/space.png" /><p>您的关注人数超出。</p></div>';
                                    break;
                                case -2:
                                    tipContent = '<div class="SD-follow-box"><img class="tipicon tip0" src="http://css.sodao.com/home/img/space.png" /><p>对方不允许被关注。</p></div>';
                                    break;
                                case -3:
                                    tipContent = '<div class="SD-follow-box"><img class="tipicon tip0" src="http://css.sodao.com/home/img/space.png" /><p>已经关注。</p></div>';
                                    break;
                                case -4:
                                    tipContent = '<div class="SD-follow-box"><img class="tipicon tip0" src="http://css.sodao.com/home/img/space.png" /><p>自己不能关注自己。</p></div>';
                                    break;
                                case -6:
                                    tipContent = '<div class="SD-follow-box"><img class="tipicon tip0" src="http://css.sodao.com/home/img/space.png" /><p>请先登录。</p></div>';
                                    break;
                                default:
                                    tipContent = "";
                            }

                            $(that).tipBox({
                                content: tipContent,
                                boxClass: "SD-tipsBox",
                                offset: 10,
                                width: "175px",
                                height: "20px",
                                closeable: true,
                                closeDelay: 3000,
                                direction: "top"
                            });

                            if (data.status == 1) {
                                $(that).removeClass().addClass("follow-success-btn").text("已关注").attr("title", "取消关注");
                            }
                            else if (data.status == 2) {
                                $(that).removeClass().addClass("follow-together-btn").text("互相关注").attr("title", "取消关注");
                            }
                        }
                    });
                }
                //要关注的人在我的黑名单里
                else if (data.status == -8) {
                    var url = "/friend/FriendOutWin?type=3&fromusername=" + visitUser + "&tousername=" + concernName;
                    sDialog.loadIFrame(url, "温馨提示", 410, 100, true);
                }
                //要关注的人把我加为黑名单
                else if (data.status == -9) {
                    var url = "/friend/FriendOutWin?type=4&fromusername=" + visitUser + "&tousername=" + concernName;
                    sDialog.loadIFrame(url, "温馨提示", 410, 100, true);
                }
            }
        });

    },
    //visitUser:当前登录用户名
    //objs:要同步状态的其他DOM对象 争对一个页面有多个关注按钮
    //hideObj:添加关注成功后要隐藏的DOM对象
    //isShowTip:是否要显示成功的提示浮层 目前在show里用到
    addFollow_V2: function(that, concernName, visitUser, objs, hideObj, isShowTip) {
        //alert(visitUser);
        if (!checkLogin(visitUser)) {//checkLogin函数 在jquery.dialog.sodao.js里
            return;
        }
        $.ajax({
            url: "http://www.sodao.com/home/friend/IsFriendOutExist?jsonp=?",
            type: "GET",
            dataType: "jsonp",
            data: { "tousername": concernName },
            success: function(data) {
                if (data.status == 0) {
                    $.ajax({
                        url: "http://www.sodao.com/home/friend/ConcernAdd_V2?jsonp=?",
                        type: "GET",
                        dataType: "jsonp",
                        data: { "concernName": concernName },
                        success: function(data) {
                            var tipContent = "";
                            switch (data.status) {
                                case 1:
                                    tipContent = '<div class="SD-follow-box"><img class="tipicon tip1" src="http://css.sodao.com/home/img/space.png" /><p>关注成功。</p></div>';
                                    break;
                                case 2:
                                    tipContent = '<div class="SD-follow-box"><img class="tipicon tip1" src="http://css.sodao.com/home/img/space.png" /><p>关注成功。</p></div>';
                                    break;
                                case 0:
                                    tipContent = '<div class="SD-follow-box"><img class="tipicon tip0" src="http://css.sodao.com/home/img/space.png" /><p>关注失败。</p></div>';
                                    break;
                                case -1:
                                    tipContent = '<div class="SD-follow-box"><img class="tipicon tip0" src="http://css.sodao.com/home/img/space.png" /><p>您的关注人数超出。</p></div>';
                                    break;
                                case -2:
                                    tipContent = '<div class="SD-follow-box"><img class="tipicon tip0" src="http://css.sodao.com/home/img/space.png" /><p>对方不允许被关注。</p></div>';
                                    break;
                                case -3:
                                    tipContent = '<div class="SD-follow-box"><img class="tipicon tip0" src="http://css.sodao.com/home/img/space.png" /><p>已经关注。</p></div>';
                                    break;
                                case -4:
                                    tipContent = '<div class="SD-follow-box"><img class="tipicon tip0" src="http://css.sodao.com/home/img/space.png" /><p>自己不能关注自己。</p></div>';
                                    break;
                                case -6:
                                    tipContent = '<div class="SD-follow-box"><img class="tipicon tip0" src="http://css.sodao.com/home/img/space.png" /><p>请先登录。</p></div>';
                                    break;
                                default:
                                    tipContent = "";
                            }

                            if (isShowTip != "false") {
                                $(that).tipBox({
                                    content: tipContent,
                                    boxClass: "SD-tipsBox",
                                    offset: 10,
                                    width: "175px",
                                    height: "20px",
                                    closeable: true,
                                    closeDelay: 3000,
                                    direction: "top"
                                });
                            }
                            //关注后 加好友的层显示
                            try {

                                $.get("/user/GetRelation", { spaceUser: concernName }, function(data) {
                                    if (data == "1") {
                                        $("#friendGuide").show();
                                        $("#friend-relation-friend").show();
                                    }
                                    else if (data == "-1") {
                                        $("#friendGuide").show();
                                        $("#friend-relation-nofriend").show();
                                    }
                                    else
                                        $("#friendGuide").hide();

                                });
                                if (hideObj != null && typeof (hideObj) != "undefined") {
                                    if ($(that).attr("id") != hideObj.attr("id")) {
                                        //alert("其他关注按钮");

                                        hideObj.hide();
                                    }
                                    else {
                                        //alert("模板页关注按钮");
                                        setTimeout(function() {  hideObj.hide(); }, 3000);
                                    } 
                                }
                            } catch (e) { }



                            if (data.status == 1) {
                                //$(that).removeClass().addClass("follow-success-btn").text("已关注").attr("title", "取消关注");
                                for (var i = 0; i < objs.length; i++) {
                                    objs[i].removeClass().addClass("follow-success-btn").text("已关注").attr("title", "取消关注");
                                    //alert(objs[i].html()+objs[i].attr("id"));
                                }
                                if (hideObj != null && typeof (hideObj) != "undefined")
                                    hideObj.removeClass().addClass("follow-success-btn").text("已关注").attr("title", "取消关注");
                            }
                            else if (data.status == 2) {
                                //$(that).removeClass().addClass("follow-together-btn").text("相互关注").attr("title", "取消关注");
                                for (var i = 0; i < objs.length; i++) {
                                    objs[i].removeClass().addClass("follow-together-btn").text("相互关注").attr("title", "取消关注");
                                    //alert(objs[i].html()+objs[i].attr("id"));
                                }
                                if (hideObj != null && typeof (hideObj) != "undefined")
                                    hideObj.removeClass().addClass("follow-together-btn").text("相互关注").attr("title", "取消关注");
                            }
                        }
                    });
                }
                //要关注的人在我的黑名单里
                else if (data.status == -8) {
                    var url = "/friend/FriendOutWin?type=3&fromusername=" + visitUser + "&tousername=" + concernName;
                    sDialog.loadIFrame(url, "温馨提示", 410, 100, true);
                }
                //要关注的人把我加为黑名单
                else if (data.status == -9) {
                    var url = "/friend/FriendOutWin?type=4&fromusername=" + visitUser + "&tousername=" + concernName;
                    sDialog.loadIFrame(url, "温馨提示", 410, 100, true);
                }
            }
        });

    },
    cancelFollow: function(that, concernName) {
        //console.log()
        //确定不再关注?
        $(that).tipBox({
            content: '<div class="SD-follow-box"><img class="tipicon tip2" src="http://css.sodao.com/home/img/space.png" /><p>确定不再关注？</p><div class="act-btnBox"><input type="button" value="确定" class="submit2" /> <input type="button" value="取消" class="submit-gray2" /></div></div>',
            boxClass: "SD-tipsBox",
            offset: 10,
            width: "175px",
            height: "50px",
            closeable: true,
            direction: "top",
            callBack: function($tipBox) {
                $tipBox.find(".submit2").click(function() {

                    $.ajax({
                        url: "http://www.sodao.com/home/friend/ConcernDel_V2?jsonp=?",
                        type: "GET",
                        dataType: "jsonp",
                        data: { "concernName": concernName },
                        success: function(data) {
                            //console.log("111");
                            $tipBox.remove();
                            $(that).removeClass().addClass("follow-none-btn").text("加关注").attr("title", "关注TA");
                        }
                    });
                });
                $tipBox.find(".submit-gray2").click(function() {
                    $tipBox.remove();
                });
            }
        });
    },
    //objs:要同步状态的其他DOM对象 争对一个页面有多个关注按钮 要显示的obj
    //isShowTip:是否要显示成功的提示浮层 目前在show里用到
    cancelFollow_V2: function(that, concernName, objs, showObj, isShowTip) {
        //console.log()
        //确定不再关注?
        if (isShowTip != "false") {
            $(that).tipBox({
                content: '<div class="SD-follow-box"><img class="tipicon tip2" src="http://css.sodao.com/home/img/space.png" /><p>确定不再关注？</p><div class="act-btnBox"><input type="button" value="确定" class="submit2" /> <input type="button" value="取消" class="submit-gray2" /></div></div>',
                boxClass: "SD-tipsBox",
                offset: 10,
                width: "175px",
                height: "50px",
                closeable: true,
                direction: "top",
                callBack: function($tipBox) {
                    $tipBox.find(".submit2").click(function() {

                        $.ajax({
                            url: "http://www.sodao.com/home/friend/ConcernDel_V2?jsonp=?",
                            type: "GET",
                            dataType: "jsonp",
                            data: { "concernName": concernName },
                            success: function(data) {
                                //console.log("111");
                                $tipBox.remove();
                                //$(that).removeClass().addClass("follow-none-btn").text("加关注").attr("title", "关注TA");
                                for (var i = 0; i < objs.length; i++) {
                                    objs[i].removeClass().addClass("follow-none-btn").text("加关注").attr("title", "关注TA");
                                    //alert(objs[i].html()+objs[i].attr("id"));
                                }
                                if (showObj != null && typeof (showObj) != "undefined")
                                    showObj.removeClass().addClass("follow-none-btn").text("加关注").attr("title", "关注TA").show();
                                //关注后 加好友的层隐藏
                                try {
                                    $("#friendGuide").hide();
                                } catch (ex) {
                                }
                            }
                        });
                    });
                    $tipBox.find(".submit-gray2").click(function() {
                        $tipBox.remove();
                    });
                }
            });
        }
        else {
            $.ajax({
                url: "http://www.sodao.com/home/friend/ConcernDel_V2?jsonp=?",
                type: "GET",
                dataType: "jsonp",
                data: { "concernName": concernName },
                success: function(data) {
                    //console.log("111");
                    //$tipBox.remove();
                    //$(that).removeClass().addClass("follow-none-btn").text("加关注").attr("title", "关注TA");
                    for (var i = 0; i < objs.length; i++) {
                        objs[i].removeClass().addClass("follow-none-btn").text("加关注").attr("title", "关注TA");
                        //alert(objs[i].html()+objs[i].attr("id"));
                    }
                    if (showObj!=null && typeof(showObj) != "undefined")
                        showObj.removeClass().addClass("follow-none-btn").text("加关注").attr("title", "关注TA").show();
                    //关注后 加好友的层隐藏
                    try {
                        $("#friendGuide").hide();
                    } catch (ex) {
                    }
                }
            });
        }
    }
};